﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using SixApp.Entities;
using SixApp.Extensions;
using SixApp.Repositories;
using SixApp.Core;

namespace SixApp
{
    public partial class FormCalc : BaseForm
    {
        private readonly FormulaRepository mFormulaRepository = new FormulaRepository();

        public FormCalc()
        {
            InitializeComponent();
        }

        private void FormCalc_Load(object sender, EventArgs e)
        {
            this.cbbPM.SelectedIndex = 0;
            this.cbbPT.SelectedIndex = 0;
            this.cbbPW.SelectedIndex = 0;
            this.cbbPH.SelectedIndex = 0;
            this.cbbPB.SelectedIndex = 0;
            this.cbbPX.SelectedIndex = 0;
            this.cbbPD.SelectedIndex = 0;
            this.cbbYW.SelectedIndex = 0;
            this.cbbPMen.SelectedIndex = 0;

            this.cbbQx.SelectedIndex = 0;
            this.cbbLx.SelectedIndex = 0;

            bool isEdit = Convert.ToInt32(this.Tag) > 0;
            if (isEdit)
            {
                DataGridViewRow formulaCurrentRow = ((FormFormula)this.Owner).FormulaCurrentRow;
                string qxValue = Convert.ToString(formulaCurrentRow.Cells["colQx"].Value).TrimIfNotNull();
                string lxValue = Convert.ToString(formulaCurrentRow.Cells["colLx"].Value).TrimIfNotNull();
                string xqsValue = Convert.ToString(formulaCurrentRow.Cells["colXqs"].Value).TrimIfNotNull();
                string csValue = Convert.ToString(formulaCurrentRow.Cells["colCs"].Value).TrimIfNotNull();
                string gsValue = Convert.ToString(formulaCurrentRow.Cells["colGs"].Value).TrimIfNotNull();

                tbFormula.Text = gsValue;
                cbbQx.SelectedIndex = cbbQx.Items.IndexOf(qxValue);
                cbbLx.SelectedIndex = cbbLx.Items.IndexOf(lxValue);
                tbXqs.Text = xqsValue;
                tbCs.Text = csValue;
            }
        }

        private void btnCalc_Click(object sender, EventArgs e) 
        {
            //calculation

        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(tbXqs.Text) || string.IsNullOrEmpty(tbCs.Text))
            {
                MessageBox.Show("请输入下期杀跟错数！", "系统提示");
                return;
            }

            //insert or update formula record
            Formula formula = new Formula() {
                cs = Convert.ToInt32(tbCs.Text),
                gs = tbFormula.Text.TrimIfNotNull(),
                Id = Convert.ToInt32(this.Tag),
                lx = cbbLx.Text,
                qx = cbbQx.Text,
                xqs = tbXqs.Text.TrimIfNotNull(),
            };

            mFormulaRepository.SaveOrUpdate(formula);

            FormFormula formFormula = (FormFormula)this.Owner;
            formFormula.BindFormula(formFormula.CurrentFormulaType);
            this.Close();
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            //平码一
            cbbPM.SelectedIndex = 0;
            
            //平一头
            cbbPT.SelectedIndex = 0;

            //平一尾
            cbbPW.SelectedIndex = 0;            

            //平一合
            cbbPH.SelectedIndex = 0;
            
            //平一波
            cbbPB.SelectedIndex = 0;
            
            //平一肖
            cbbPX.SelectedIndex = 0;
            
            //平一段
            cbbPD.SelectedIndex = 0;            

            //一合尾
            cbbYW.SelectedIndex = 0;            

            //平一门
            cbbPMen.SelectedIndex = 0;            

            //总分数
            tbZfs.Text = string.Empty;

            //总分尾
            tbZfw.Text = string.Empty;            

            //总分合
            tbZfh.Text = string.Empty;            

            //总合尾
            tbZhw.Text = string.Empty;            

            //上期数
            tbSqs.Text = string.Empty;

            //期数尾
            tbQsw.Text = string.Empty;            

            //期数合
            tbQsh.Text = string.Empty;            

            //期合尾
            tbQhw.Text = string.Empty;
            
            tbFormula.Text = string.Empty;
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            //generate formula
            StringBuilder sbFormula = new StringBuilder();

            //平码一
            if (cbbPM.SelectedIndex > 0)
            {
                sbFormula.Append("+" + cbbPM.Text);
            }

            //平一头
            if (cbbPT.SelectedIndex > 0)
            {
                sbFormula.Append("+" + cbbPT.Text);
            }

            //平一尾
            if (cbbPW.SelectedIndex > 0)
            {
                sbFormula.Append("+" + cbbPW.Text);
            }

            //平一合
            if (cbbPH.SelectedIndex > 0)
            {
                sbFormula.Append("+" + cbbPH.Text);
            }

            //平一波
            if (cbbPB.SelectedIndex > 0)
            {
                sbFormula.Append("+" + cbbPB.Text);
            }

            //平一肖
            if (cbbPX.SelectedIndex > 0)
            {
                sbFormula.Append("+" + cbbPX.Text);
            }

            //平一段
            if (cbbPD.SelectedIndex > 0)
            {
                sbFormula.Append("+" + cbbPD.Text);
            }

            //一合尾
            if (cbbYW.SelectedIndex > 0)
            {
                sbFormula.Append("+" + cbbYW.Text);
            }

            //平一门
            if (cbbPMen.SelectedIndex > 0)
            {
                sbFormula.Append("+" + cbbPMen.Text);
            }

            //总分数
            if (!string.IsNullOrEmpty(tbZfs.Text))
            {
                sbFormula.Append("+" + tbZfs.Text);
            }

            //总分尾
            if (!string.IsNullOrEmpty(tbZfw.Text))
            {
                sbFormula.Append("+" + tbZfw.Text);
            }

            //总分合
            if (!string.IsNullOrEmpty(tbZfh.Text))
            {
                sbFormula.Append("+" + tbZfh.Text);
            }

            //总合尾
            if (!string.IsNullOrEmpty(tbZhw.Text))
            {
                sbFormula.Append("+" + tbZhw.Text);
            }

            //上期数
            if (!string.IsNullOrEmpty(tbSqs.Text))
            {
                sbFormula.Append("+" + tbSqs.Text);
            }

            //期数尾
            if (!string.IsNullOrEmpty(tbQsw.Text))
            {
                sbFormula.Append("+" + tbQsw.Text);
            }

            //期数合
            if (!string.IsNullOrEmpty(tbQsh.Text))
            {
                sbFormula.Append("+" + tbQsh.Text);
            }

            //期合尾
            if (!string.IsNullOrEmpty(tbQhw.Text))
            {
                sbFormula.Append("+" + tbQhw.Text);
            }

            if (sbFormula.Length > 0)
            {
                sbFormula.Remove(0, 1); //remove first "+"
            }            

            tbFormula.Text = sbFormula.ToString();
        }
    }
}
